//
//  CMDBDataBaseSummary.swift
//  CMDB
//
//  Created by 曹桂祥 on 2017/5/24.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBDataBaseSummaryView: CMDBBaseView {
    
    var summaryArr : [String]? {
        
        didSet{
            
            guard let summaryArr = summaryArr else { return }
            
            tempStr = ""
            
            for (indxe,condition) in summaryArr.enumerate() {
                
                if indxe == summaryArr.count - 1 {
                    
                    tempStr = tempStr.stringByAppendingString(" \(condition)")
                    
                }else{
                    tempStr = tempStr.stringByAppendingString(" \(condition) ・ ")
                }
                
            }
            
            showLabel.text = tempStr
            
        }
        
    }
    
    var tempStr : String = ""
    
    var showLabel : UILabel!
    
    var arrowBtn : UIButton!
    
    //自定义构造函数
    init(frame: CGRect, summaryArr : [String]) {
        
        self.summaryArr = summaryArr
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension CMDBDataBaseSummaryView{
    
    func setupUI(){
        
        //1.处理数据
        tempStr = ""
        
        for (indxe,condition) in summaryArr!.enumerate() {
            
            if indxe == summaryArr!.count - 1 {
               
               tempStr = tempStr.stringByAppendingString(" \(condition)")
                
            }else{
               tempStr = tempStr.stringByAppendingString(" \(condition) ・ ")
            }
            
        }
        
        //2.添加label
        showLabel       = UILabel(strColor:orangeColor , string: tempStr, strFont: CMDB_16_FONT)
        
        showLabel.frame = CGRect(x: KleftMargin, y: 5, width: CMDB_Screen_Width - KleftMargin * 3, height: 34)
        
        addSubview(showLabel)
        
        //3.添加button
        arrowBtn        = UIButton(imageName: "arrow", selectedImage: "arrow")
        
        arrowBtn.addTarget(self, action: #selector(arrowBtnClick), forControlEvents: .TouchUpInside)
        
        arrowBtn.frame  = CGRect(x: CGRectGetMaxX(showLabel.frame) + 2, y: KleftMargin, width: arrowBtn.width, height: arrowBtn.height)
        
        addSubview(arrowBtn)
      
        //3.添加分割线
        
        let singleLine = UIView.CreateLine(CGRect(x: 0, y: 43.5, width: CMDB_Screen_Width, height: 0.5), lineColor: word12Color)
        
        addSubview(singleLine)
    }
    
}

extension CMDBDataBaseSummaryView{
    
    @objc func arrowBtnClick(){
        
        mDelegate?.onItemClickType!("DataBaseConditionShowArrowBtnClick", data: nil, indexPath: nil)
        
    }
    
}
